package com.irisking.ikaisdk.eventListener;

import java.util.HashMap;
import java.util.Map;

public class EventManager {

    /**
     * 事件远
     */
    private Map<Class, EventListener> deviceEventHandlerMap  =  new HashMap<>();

    /**
     * 注册监听事件
     * @param listener
     */
    public void addEventListener(EventListener listener){
        Class<?>[]  classes = listener.getClass().getInterfaces();
        if (classes!=null){
            Class event = classes[0];
            if (deviceEventHandlerMap.get(event) == null ){
                deviceEventHandlerMap.put(event,listener);
            }else {
                deviceEventHandlerMap.remove(event);
                deviceEventHandlerMap.put(event,listener);
            }
        }
    }

    /**
     * 获取事件
     * @param eventName
     * @return
     */
    public EventListener getEventListener(Class eventName){
        return deviceEventHandlerMap.get(eventName);
    }

}
